package com.example.boke.controller.admin;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.boke.entity.Blog;
import com.example.boke.entity.BlogTag;
import com.example.boke.entity.Tag;
import com.example.boke.mapper.BlogMapper;
import com.example.boke.mapper.BlogTagMapper;
import com.example.boke.mapper.TagMapper;
import com.example.boke.service.impl.BlogTagServiceImpl;
import com.example.boke.service.impl.TagServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;


import javax.servlet.http.HttpSession;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author admin
 * @since 2021-05-03
 */
@Controller
@RequestMapping("/admin")
public class TagController {

    @Autowired
    private TagMapper TagMapper;

    @Autowired
    private TagServiceImpl tagServiceimpl;

    @Autowired
    private BlogTagMapper blogTagMapper;

    @Autowired
    private BlogTagServiceImpl blogTagServiceimpl;

    @Autowired
    private BlogMapper blogMapper;

    @GetMapping(value = "/tag")
    public String tag(Integer pc, HttpSession session , Model model)
    {
        if (pc == null)
        {
            pc = 1;
        }

        long userId = (long) session.getAttribute("id");

        Page<Tag> tagPage = new Page<>(pc,5);

        QueryWrapper<Tag> tagQueryWrapper = new QueryWrapper<Tag>();

        tagQueryWrapper.eq("user_id",userId).orderByDesc("id");

        TagMapper.selectPage(tagPage,tagQueryWrapper);

        model.addAttribute("taglist", tagPage.getRecords());
        model.addAttribute("countpage", tagPage.getPages());//总页数
        model.addAttribute("pc", tagPage.getCurrent());
        model.addAttribute("hasPrevious",tagPage.hasPrevious());   //是否有上一页
        model.addAttribute("hasNext",tagPage.hasNext());       //是否有下一页

        return "admin/tag";
    }

    @GetMapping(value = "/addtag")
    public String addtag()
    {
        return "admin/tag-input";
    }

    @PostMapping(value = "/savetag")
    public String savetag(String name , HttpSession session, RedirectAttributes redirectAttributes) {

        QueryWrapper<Tag> tagQueryWrapper = new QueryWrapper<>();

        long userId = (long)session.getAttribute("id");

        tagQueryWrapper.eq("name",name).eq("user_id",userId);

        Integer integer = TagMapper.selectCount(tagQueryWrapper);

        if ( integer != 0)
        {
            redirectAttributes.addFlashAttribute("message","标签已存在");
            redirectAttributes.addFlashAttribute("name",name);
            return "redirect:/admin/addtag";
        }

        Tag tag = new Tag();

        tag.setName(name);

        tag.setUserId(userId);

        boolean save = tagServiceimpl.save(tag);

        if (save)
        {
            redirectAttributes.addFlashAttribute("message","新增标签成功");
            return "redirect:/admin/tag";

        }else {
            redirectAttributes.addFlashAttribute("message","新增标签失败");
            return "redirect:/admin/tag";
        }

    }

    @GetMapping(value = "/removetag")
    public String removetag(HttpSession sessino , Long tagId,RedirectAttributes redirectAttributes) {
        Long UserId = (Long) sessino.getAttribute("id");

        QueryWrapper<BlogTag> blogTagQueryWrapper = new QueryWrapper<>();

        blogTagQueryWrapper.eq("tags_id", tagId).eq("user_id",UserId);

        Integer result = blogTagMapper.selectCount(blogTagQueryWrapper);
        System.out.println("结果：" + result);
        if (result != 0)
        {
            redirectAttributes.addFlashAttribute("message","删除失败，有文章引用该标签");
            return "redirect:/admin/tag";
        }

        UpdateWrapper<Tag> tagUpdateWrapper = new UpdateWrapper<>();

        tagUpdateWrapper.eq("user_id", UserId).eq("id", tagId);

        tagServiceimpl.remove(tagUpdateWrapper);

        return "redirect:/admin/tag";

    }

    @GetMapping(value = "/updatetagone")
    public String updatetagone(String tagName , Long tagId,RedirectAttributes redirectAttributes)
    {

        redirectAttributes.addFlashAttribute("name",tagName);
        redirectAttributes.addFlashAttribute( "tagId",tagId);

        return "redirect:/admin/addtag";
    }

    @PostMapping(value = "/updatetagtwo")
    public String updatetagtwo(String name,Long tagId , HttpSession session,RedirectAttributes redirectAttributes)
    {

        UpdateWrapper<Tag> tagUpdateWrapper = new UpdateWrapper<>();
        QueryWrapper<Tag> tagQueryWrapper = new QueryWrapper<>();

        Long userId = (Long) session.getAttribute("id");

        tagQueryWrapper.eq("name",name).eq("user_id",userId);

        Integer integer = TagMapper.selectCount(tagQueryWrapper);

        if ( integer != 0)
        {
            redirectAttributes.addFlashAttribute("message","标签已存在了");

            redirectAttributes.addFlashAttribute("name",name);
            redirectAttributes.addFlashAttribute("tagId",tagId);
            return "redirect:/admin/addtag";
        }


        tagUpdateWrapper.set("name",name).eq("user_id",userId).eq("id",tagId);

        int update = TagMapper.update(null, tagUpdateWrapper);

        if (update == 0)
        {
            redirectAttributes.addFlashAttribute("message","修改失败");
            return "redirect:/admin/addtag";
        }

        redirectAttributes.addFlashAttribute("message","修改成功");

        return "redirect:/admin/tag";

    }

}
